(function($) {
	$.extend($.fn.datagrid.methods, {
		fixRowNumber: function (jq) {
			return jq.each(function () {
				var panel = $(this).datagrid("getPanel");
				var clone = $(".datagrid-cell-rownumber", panel).last().clone();
				clone.css({
					"position": "absolute",
					left: -1000
				}).appendTo("body");
				var width = clone.width("auto").width();
				if (width > 25) {
					$(".datagrid-header-rownumber,.datagrid-cell-rownumber", panel).width(width + 3);
					$(this).datagrid("resize");
					clone.remove();
				}
			});
		},
		// 扩展 easyui-datagrid 的自定义方法；获取 easyui-datagrid 中当前页指定行的 rowData；该函数定义如下参数：
		// index: 表示要获取的 rowData 所在当前页的行索引号，从 0 开始；
		// 返回值：如果当前页存在 index 指示的行，则返回该行的行数据对象（JSON Object 格式）；否则返回 undefined。
		getRow: function (jq, index) {
			if (!$.isNumeric(index) || index < 0) { return undefined; }
			var t = $(jq[0]);
			var rows = t.datagrid("getRows");
			return rows[index];
		},
		// 加载空数据，并清除选中值
		clearData: function (jq) {
			var t = $(jq[0]);
			t.datagrid('loadData', { total: 0, rows: [] });
			t.datagrid("clearSelections");
			t.datagrid("clearChecked");
		}
	});
	
	$.extend($.fn.treegrid.methods, {
		// 加载空数据，并清除选中值
		clearData: function (jq) {
			var t = $(jq[0]);
			t.treegrid('loadData', { total: 0, rows: [] });
			t.treegrid("clearSelections");
			t.treegrid("clearChecked");
		}
	});
	
	$.fn.combo.defaults.editable=false;
	$.fn.combobox.defaults.limitToList=true;
	$.fn.combotree.defaults.editable=false;
	$.fn.datebox.defaults.editable=false;
	$.fn.datetimebox.defaults.editable=false;
	$.fn.datetimespinner.defaults.showSeconds=true;
	
	var selectIndex = function(target, index) {
		var t = $(target), data = t.combobox("getData"), opts = t.combobox("options");
		if (data.length > index && index >= 0) {
			t.combobox('select', data[index][opts.valueField]);
		}
	}
	$.extend($.fn.combobox.methods, {
		// 扩展  combobox 自定义方法，按下标选中
		selectIndex: function(jq, index) {
			selectIndex(jq[0], index);
		}
	});
	
	var lookup = function(target, mode, delay, url, method, queryParams, required, editable, disabled, readonly, columns, idField, textField, sortName, sortOrder, multiSort, pagination, pageSize, rownumbers, panelWidth, panelHeight, value, callShowPanel, callHidePanel, callBeforeLoad, callClick, callSelect, callUnselect, callChange, callLoadSuccess, callLoadError, editRowIndex, combogrid_OrgValue, combogrid_Columns, combogrid_NewValue) {
		$(target).combogrid({
			mode: mode,
			delay: delay,
			url: url,
			method: method,
			queryParams: queryParams,
			required: required,
			editable: editable,
			disabled: disabled,
			readonly: readonly,
			idField: idField,
			textField: textField,
			sortName: sortName,
			sortOrder: sortOrder,
			multiSort: multiSort,
			pagination: pagination,
			pageSize: pageSize,
			rownumbers: rownumbers,
			panelWidth: panelWidth,
			panelHeight: panelHeight,
			value: value,
			fitColumns: true,
			columns: columns,
			onShowPanel: function() {
				combogrid_OrgValue = $(this).combogrid('getValue');
				combogrid_Columns = $(this).combogrid('options').columns[0];
				if (callShowPanel && typeof(callShowPanel) === "function") {
					callShowPanel();
				}
			},
			onHidePanel: function() {
				combogrid_NewValue = $(this).combogrid('getValue');
				var selected = $(this).combogrid('grid').datagrid('getSelected');
				if (selected == null) {
					if (!combogrid_OrgValue || combogrid_OrgValue != combogrid_NewValue) {
						$(this).combogrid('setValue', '');
					}
				}
				var q = $(this).combogrid('grid').datagrid('options').queryParams.q;
				if (q) {
					$(this).combogrid('grid').datagrid('options').queryParams.q = '';
					$(this).combogrid('grid').datagrid('reload');
				}
				if (callHidePanel && typeof(callHidePanel) === "function") {
					callHidePanel();
				}
			},
			onBeforeLoad: function(param){
				if (callBeforeLoad && typeof(callBeforeLoad) === "function") {
					return callBeforeLoad(param);
				}
			},
			onClick: function(record) {
				if (callClick && typeof(callClick) === "function") {
					callClick(record);
				}
			},
			onSelect: function(index, row) {
				if (callSelect && typeof(callSelect) === "function") {
					callSelect(index, row, editRowIndex);
				}
			},
			onUnselect: function(index, row) {
				if (callUnselect && typeof(callUnselect) === "function") {
					callUnselect(index, row, editRowIndex);
				}
			},
			onChange: function(newValue, oldValue) {
				combogrid_NewValue = newValue;
				if (callChange && typeof(callChange) === "function") {
					callChange(newValue, oldValue, editRowIndex);
				}
			},
			onLoadSuccess: function(data) {
				if (data.total > 0) {
					if (typeof(combogrid_NewValue) != "undefined" && combogrid_NewValue) {
						var n = (combogrid_NewValue + '').toLowerCase();
						if (typeof(combogrid_Columns) != "undefined" && n) {
							for (var x in combogrid_Columns) {
								var y = data.rows[0][combogrid_Columns[x].field];
								var z = Object.prototype.toString.call(y);
								if (z == '[object String]') {
									if (y.toLowerCase() == n) $(this).datagrid("selectRow", 0);
								} else if (z == '[object Number]') {
									if (y == n) $(this).datagrid("selectRow", 0);
								}
							}
						}
					}
				}
				if (callLoadSuccess && typeof(callLoadSuccess) === "function") {
					callLoadSuccess(data);
				}
			},
			onLoadError: function() {
				if (callLoadError && typeof(callLoadError) === "function") {
					callLoadError();
				}
			},
			editRowIndex: editRowIndex
		});
	}

	$.extend($.fn.combogrid.methods, {
		// 扩展 combogrid 自定义方法，下拉数据源
		lookup: function(jq, param) {
			var mode = 'remote';
			var delay = 1000;
			var url;
			var method = 'post';
			var queryParams = {};
			var required = false;
			var editable = true;
			var disabled = false;
			var readonly = false;
			var idField = 'id';
			var textField = 'no';
			var sortName = 'no';
			var sortOrder = 'asc';
			var multiSort = false;
			var pagination = true;
			var pageSize = 20;
			var rownumbers = true;
			var panelWidth = 400;
			var panelHeight = 319;
			var value;
			var columns = [ [
				{field:'no', title:$.fn.combogrid.defaults.columnNo, width:150, sortable:true},
				{field:'name', title:$.fn.combogrid.defaults.columnName, width:250, sortable:true}
			] ];
			var editRowIndex;
			var callShowPanel;
			var callHidePanel;
			var callBeforeLoad;
			var callClick;
			var callSelect;
			var callUnselect;
			var callChange;
			var callLoadSuccess;
			var callLoadError;
			if (jq.length > 0) {
				var options = $.fn.combogrid.parseOptions(jq[0]);
				if (options.mode)
					mode = options.mode;
				if (options.delay)
					delay = options.delay;
				if (options.method)
					method = options.method;
				if (options.queryParams)
					queryParams = options.queryParams;
				if (typeof options.required != "undefined")
					required = options.required;
				if (options.columns)
					columns = options.columns;
				if (options.idField)
					idField = options.idField;
				if (options.textField)
					textField = options.textField;
				if (options.sortName)
					sortName = options.sortName;
				if (options.sortOrder)
					sortOrder = options.sortOrder;
				if (typeof options.multiSort != "undefined")
					multiSort = options.multiSort;
				if (typeof options.pagination != "undefined")
					pagination = options.pagination;
				if (options.pageSize)
					pageSize = options.pageSize;
				if (typeof options.rownumbers != "undefined")
					rownumbers = options.rownumbers;
				if (typeof options.editable != "undefined")
					editable = options.editable;
				if (typeof options.disabled != "undefined")
					disabled = options.disabled;
				if (typeof options.readonly != "undefined")
					readonly = options.readonly;
				if (options.panelWidth)
					panelWidth = options.panelWidth;
				if (options.panelHeight)
					panelHeight = options.panelHeight;
				if (typeof options.value != "undefined")
					value = options.value;
				if (typeof options.editRowIndex != "undefined")
					editRowIndex = options.editRowIndex;
				if (typeof options.multiple != "undefined" && options.multiple == true) {
					columns = [ [
						{field:'id', checkbox:true},
						{field:'no', title:$.fn.combogrid.defaults.columnNo, width:150, sortable:true},
						{field:'name', title:$.fn.combogrid.defaults.columnName, width:250, sortable:true}
					] ];
				}
				if (options.onShowPanel)
					callShowPanel = options.onShowPanel;
				if (options.onHidePanel)
					callHidePanel = options.onHidePanel;
				if (options.onBeforeLoad)
					callBeforeLoad = options.onBeforeLoad;
				if (options.onClick)
					callClick = options.onClick;
				if (options.onSelect)
					callSelect = options.onSelect;
				if (options.onUnselect)
					callUnselect = options.onUnselect;
				if (options.onChange)
					callChange = options.onChange;
				if (options.onLoadSuccess)
					callLoadSuccess = options.onLoadSuccess;
				if (options.onLoadError)
					callLoadError = options.onLoadError;
			}
			if (param == undefined) {
				return;
			} else if (Object.prototype.toString.call(param) == "[object String]") {
				url = param;
			} else {
				url = param.url;
				if (param.mode)
					mode = param.mode;
				if (param.delay)
					delay = param.delay;
				if (param.method)
					method = param.method;
				if (param.queryParams)
					queryParams = param.queryParams;
				if (typeof param.required != "undefined")
					required = param.required;
				if (param.columns)
					columns = param.columns;
				if (param.idField)
					idField = param.idField;
				if (param.textField)
					textField = param.textField;
				if (param.sortName)
					sortName = param.sortName;
				if (param.sortOrder)
					sortOrder = param.sortOrder;
				if (typeof param.multiSort != "undefined")
					multiSort = param.multiSort;
				if (typeof param.pagination != "undefined")
					pagination = param.pagination;
				if (param.pageSize)
					pageSize = param.pageSize;
				if (typeof param.rownumbers != "undefined")
					rownumbers = param.rownumbers;
				if (typeof param.editable != "undefined")
					editable = param.editable;
				if (typeof param.disabled != "undefined")
					disabled = param.disabled;
				if (typeof param.readonly != "undefined")
					readonly = param.readonly;
				if (param.panelWidth)
					panelWidth = param.panelWidth;
				if (param.panelHeight)
					panelHeight = param.panelHeight;
				if (typeof param.value != "undefined")
					value = param.value;
				if (typeof param.editRowIndex != "undefined")
					editRowIndex = param.editRowIndex;
				if (param.onShowPanel)
					callShowPanel = param.onShowPanel;
				if (param.onHidePanel)
					callHidePanel = param.onHidePanel;
				if (param.onBeforeLoad)
					callBeforeLoad = param.onBeforeLoad;
				if (param.onClick)
					callClick = param.onClick;
				if (param.onSelect)
					callSelect = param.onSelect;
				if (param.onUnselect)
					callUnselect = param.onUnselect;
				if (param.onChange)
					callChange = param.onChange;
				if (param.onLoadSuccess)
					callLoadSuccess = param.onLoadSuccess;
				if (param.onLoadError)
					callLoadError = param.onLoadError;
			}
			lookup(jq[0], mode, delay, url, method, queryParams, required, editable, disabled, readonly, columns, idField, textField, sortName, sortOrder, multiSort, pagination, pageSize, rownumbers, panelWidth, panelHeight, value, callShowPanel, callHidePanel, callBeforeLoad, callClick, callSelect, callUnselect, callChange, callLoadSuccess, callLoadError, editRowIndex);
		}
	});

	$.extend($.fn.datebox.methods, {
		setMode: function(jq, param) {
			if (param == undefined) {
				return;
			} else if (param == 'month') { //月模式
				$(jq[0]).datebox({
					onShowPanel: function () {
						var t = $(jq[0]);
						var p = t.datebox('panel'), tds = false, span = p.find('.calendar-title span');
						p.find('.calendar-menu').show();
						span.removeClass('calendar-text');
						if (!tds)
							setTimeout(function() {
								tds = p.find('div.calendar-menu-month-inner td');
								tds.click(function(e) {
									e.stopPropagation(); //禁止冒泡
									var year = /\d{4}/.exec(span.html())[0], month = parseInt($(this).attr('abbr'), 10);
									t.datebox('hidePanel').datebox('setValue', year + '-' + month); //设置日期
								});
							}, 0);
					},
					parser: function (s) {
						if (!s) return new Date();
						var arr = s.split('-');
						return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
					},
					formatter: function (d) {
						var y = d.getFullYear();
						var m = d.getMonth() + 1;
						return y + '-' + (m < 10 ? ('0' + m) : m);
					}
				});
			}
		}
	});
	
	$.fn.serializeObject = function() {
		var o = {};
		var a = this.serializeArray();
		$.each(a, function() {
			if (o[this.name]) {
				if (!o[this.name].push) {
					o[this.name] = [o[this.name]];
				}
				o[this.name].push(this.value || '');
			} else {
				o[this.name] = this.value || '';
			}
		});
		return o;
	};
})(jQuery);